package org.ms.resumeservice.controller;

import org.ms.resumeservice.model.Resume;
import org.ms.resumeservice.service.resumeService;
import org.ms.resumeservice.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

@RestController
public class resumeController {
    @Autowired
    resumeService resumeService;

//    @PostMapping("/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseResult upload(@RequestParam MultipartFile file,
                                 @RequestParam Long userId) {
        if (file.isEmpty() || !file.getOriginalFilename().endsWith(".pdf")) {
            return new ResponseResult("404","请上传PDF文件");
        }
        try {
            Resume resume = new Resume();
            resume.setUserId(userId);
            resume.setFileName(file.getOriginalFilename());
            resume.setFileContent(file.getBytes());
            resume.setCreateTime(new Date());
            resumeService.save(resume);
            return new ResponseResult("200","上传成功");
        } catch (IOException e) {
            return new ResponseResult("404","上传失败：" );
        }
    }

}
